package cn.tedu.hospital.outpatient.service.impl;

import cn.tedu.hospital.outpatient.mapper.OutpatientMapper;
import cn.tedu.hospital.outpatient.pojo.dto.OutpatientDto;
import cn.tedu.hospital.outpatient.pojo.dto.OutpationQuery;
import cn.tedu.hospital.outpatient.pojo.entity.Outpatient;
import cn.tedu.hospital.outpatient.service.OutpatientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class OutpatientServiceImpl implements OutpatientService {

    @Autowired
    private OutpatientMapper outpatientMapper;
    @Override
    public void saveOutpatient(OutpatientDto outpatientDto) {
        log.debug("业务层：保存预约单参数：{}", outpatientDto);
        if (outpatientDto.getAppointmentId() ==null){
            Outpatient outpatient = new Outpatient();
//            将dto中的属性值复制到entity中
            BeanUtils.copyProperties(outpatientDto,outpatient);
//            生成创建时间
            outpatient.setCreateTime(new Date());
            outpatient.setAppointmentState(20);
//            todo 后续排队号需要改sql 根据科室和医生生成
            long queuingNumber =outpatientMapper.slectQueuingNumber();
            log.debug("业务层：查询当天挂号单未就诊状态序号：{}",queuingNumber+1);
            outpatient.setQueuingNumber(queuingNumber+1);
//            todo 暂定一个新增患者方法
//            todo 表里缺一个更改时间
            outpatientMapper.saveOutpatient(outpatient);
        }

    }

    @Override
    public List<Outpatient> selectOutpatient(OutpationQuery outpationQuery) {
        log.debug("业务层：查询预约单参数：{}", outpationQuery);
        return  outpatientMapper.selectOutpatient(outpationQuery);
    }

}
